home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-13
/
me_cd22.zip
/
MC2MUTT.ZIP
/
QSORT.MUT
< prev
next >
Wrap
Text File
|
1992-04-27
|
2KB
|
55 lines
; Quick sort. From Horowitz & Sahni pg 347
; Sort list[m..n] into assending order
; swap is a routine that interchanges list[a] with list[b]
; cmp compares list[a] with list[b] and returns:
; negative if list[a] < list[b]
; positive if list[a] > list[b]
; zero if list[a] = list[b]
(defun
Qsort (list)(int m n)(pointer defun swap cmp)
{
(int i j)
(if (>= m n)(done))
(i m)(j (+ n 1))
(while TRUE
{
(while { (+= i 1)(and (<= i n) (< (cmp list i m) 0)) } ())
(while { (-= j 1)(> (cmp list j m) 0) } ())
(if (< i j) (swap list i j) (break))
})
(swap list m j)
(Qsort list m (- j 1) swap cmp)
(Qsort list (+ j 1) n swap cmp)
}
)
(defun
swap (array int list 1)(int a b) ; swap 2 elements
{
(int tmp)
(tmp (list a))(list a (list b))(list b tmp)
}
cmp (array int list 1)(int a b) ; compare 2 elements
{ (- (list a)(list b)) }
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;; test ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(include random.mut)
(array int list 501) (int n j)
(n (atoi (ask "n = ")))
;(srand 123)
(for (j 0) (< j n)(+= j 1) (list j (rand)))
(for (j 0)(< j n)(+= j 1) (msg "list[" j "] = " (list j)))
(Qsort list 0 (- n 1) (floc swap) (floc cmp))
(msg "--------------------------")
(for (j 0)(< j n)(+= j 1) (msg "list[" j "] = " (list j)))